构造字符串

// 需要添加string命名空间 #include <string>

    // 1
    string one("shenjun");
    cout << one << endl;    // shenjun
    printf("name : %s\n", one.data());
    printf("name : %s\n", one.c_str());
    char n[10];
    one.copy(n, one.length());
    *(n+one.length()) = '\0';
    printf("name = %s\n", n);

    // 2
    string two(20, '$');    // 创建重复的20个字符
    cout << two << endl;    // $$$$$$$$$$$$$$$$$$$$

    // 3
    string three(one);
    cout << three << endl;  // shenjun

    // 4
    one += "OOP!";
    cout << one << endl;    // shenjunOOP!

    // 5
    two = "Sorry! That was ";
    cout << two << endl;    // Sorry! That was

    // 6
    three[0] = 'S';
    cout << three << endl;  // Shenjun

    // 7
    string four;
    four = two + three;
    cout << four << endl;   // Sorry! That was Shenjun

    // 8
    char alls[] = "All's well that ends well";
    string five (alls, 20); // 从字符数组中取20个字符
    cout << five << endl;   // All's well that ends

    // 9 注意 alls是数组名(地址),这里不能用其他对象名
    string six(alls + 6, alls + 10); // 取地址为alls+6及到地址alls+10之间的字符串 [alls+6, alls+10)
    cout << six << endl;  // well

    // 10
    string seven(&five[6], &five[10]); // 取地址five[6]及地址five[10]之间的字符串 [&five[6], &five[10])
    cout << seven << endl;// well

    // 11
    string eight("12345678901234567890123", 7, 3);  // 从第几位开始取,取多少个字符
    cout << eight << endl;   // 890